#include "stdafx.h"
#include <SolarFileReader.h>
#include <Macro.h>
#include <ProgramCommon/WzDataType.h>
#include ".\mapinfoloader.h"
#include "FieldInfo.h"
#include <MapInfoParser.h>

MapInfoLoader::MapInfoLoader()
{
	m_pMapInfoHashTable		= new SolarHashTable<MapInfo *>;
	m_pMapInfoHashTable->Initialize(100);
	m_pFieldInfoHashTable	= new SolarHashTable<FieldInfo *>;
	m_pFieldInfoHashTable->Initialize(100);
	
}

MapInfoLoader::~MapInfoLoader()
{
	SAFE_DELETE( m_pMapInfoHashTable );
	SAFE_DELETE( m_pFieldInfoHashTable );
}

VOID MapInfoLoader::UnloadInfo()
{
	if( m_pMapInfoHashTable )
	{
		MapInfo * pMapInfo = NULL;
		m_pMapInfoHashTable->SetFirst();
		while( (pMapInfo = m_pMapInfoHashTable->GetNext()) != NULL )
		{
			/*
			FieldInfo * pFieldInfo = NULL;
			pMapInfo->setFirst();
			while( (pFieldInfo = pMapInfo->getNext()) != NULL )
			{
				SAFE_DELETE(pFieldInfo);
			}
			*/
			SAFE_DELETE( pMapInfo );
		}

		m_pMapInfoHashTable->RemoveAll();
	}

	if( m_pFieldInfoHashTable )
	{
		FieldInfo * pFieldInfo = NULL;
		m_pFieldInfoHashTable->SetFirst();
		while( (pFieldInfo = m_pFieldInfoHashTable->GetNext()) != NULL )
		{
			SAFE_DELETE( pFieldInfo );
		}

		m_pFieldInfoHashTable->RemoveAll();
	}
}

VOID MapInfoLoader::EstablishInfo( WORD wVillageSectorSize, WORD wRoomSectorSize )
{
	MapInfo * pMapInfo		= NULL;
	FieldInfo * pFieldInfo	= NULL;
	sMAPINFO * _pMapInfo	= NULL;
	MapInfoParser * pParser = MapInfoParser::Instance();

	BOOL bViewport = FALSE;
	WORD wSectorSize = wRoomSectorSize;
	MAPINFO_HASH & rHash = pParser->GetMapInfoHash();
	for( MAPINFO_HASH_ITR it = rHash.begin() ; it != rHash.end() ; ++it )
	{
		_pMapInfo = (*it);
//	pParser->SetFirst();
//	while( (_pMapInfo = pParser->GetNext()) != NULL )
//	{
		pMapInfo = new MapInfo;
		pMapInfo->SetMapInfo( _pMapInfo );

		if( _pMapInfo->byMKind == eZONETYPE_VILLAGE )	
		{
			wSectorSize = wVillageSectorSize;
			bViewport = TRUE;
		}
	
		for(int i=0;i<sMAPINFO::MAX_FIELD_NUM;++i)
		{
			if( 0 != _pMapInfo->FCode[i])
			{
				pFieldInfo = _GetFieldInfo( _pMapInfo->FCode[i] );
				if( !pFieldInfo )
				{
					pFieldInfo = new FieldInfo;
					_AddFieldInfo( _pMapInfo->FCode[i], pFieldInfo );
					ASSERT( MapInfoParser::Instance()->GetFieldInfo( _pMapInfo->FCode[i] ) );
					pFieldInfo->Create( MapInfoParser::Instance()->GetFieldInfo( _pMapInfo->FCode[i] ) );
					pFieldInfo->Load();

					pFieldInfo->EstablishSectorInfo( wSectorSize, bViewport );
				}
				pMapInfo->AddFieldInfo( pFieldInfo );
			}
		}

		ASSERT( NULL == m_pMapInfoHashTable->GetData( pMapInfo->GetMapCode() ) );
		m_pMapInfoHashTable->Add( pMapInfo, pMapInfo->GetMapCode() );

		bViewport = FALSE;
	}
}

MapInfo * MapInfoLoader::GetMapInfo( MAPID MapID )
{
	return m_pMapInfoHashTable->GetData(MapID);
}

FieldInfo * MapInfoLoader::_GetFieldInfo( CODETYPE FieldCode )
{
	return m_pFieldInfoHashTable->GetData( FieldCode );
}
VOID MapInfoLoader::_AddFieldInfo( CODETYPE FieldCode, FieldInfo * pInfo )
{
	ASSERT( m_pFieldInfoHashTable->GetData( FieldCode ) == NULL );
	m_pFieldInfoHashTable->Add( pInfo, FieldCode );
}

